package com.feeyo.groundservice.checkin.fragment

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.ui.platform.ComposeView
import com.feeyo.groundservice.base_util.FyLog
import com.feeyo.groundservice.base_util.formatTimeYMD
import com.feeyo.groundservice.base_util.toSeconds
import com.feeyo.groundservice.base_util.toastCenter
import com.feeyo.groundservice.checkin.constant.CheckInPath
import com.feeyo.groundservice.checkin.constant.TOKEN
import com.feeyo.groundservice.checkin.constant.UID
import com.feeyo.groundservice.checkin.constant.dataInitFinishedFlag
import com.feeyo.groundservice.checkin.constant.resetFlag
import com.feeyo.groundservice.checkin.constant.token
import com.feeyo.groundservice.checkin.constant.uid
import com.feeyo.groundservice.checkin.fragment.view.CheckInMainView
import com.feeyo.groundservice.composewidget.util.pxToDp
import com.feeyo.groundservice.time_address.popup.TimePickerPopup
import com.therouter.TheRouter
import com.therouter.router.Autowired
import com.therouter.router.Route

/**
 *作者：70893 on 2025-10-23
 *描述：值机页面
 */
@Route(path = CheckInPath.CheckInFragment)
class CheckInFragment : BaseCalendarFragment() {

    @Autowired(name = UID)
    @JvmField
    var userUid: String? = null

    @Autowired(name = TOKEN)
    @JvmField
    var accessToken: String? = null

    @Autowired(name = "topPadding")
    @JvmField
    var topPadding: Int = 0


    //底部日期
    private val currentDate = mutableLongStateOf(System.currentTimeMillis().toSeconds())

    override fun calendarDateChange(date: String, time: Long) {
        FyLog.e(TAG, "date:$date,time:$time")
        resetFlag()
        currentDate.longValue = time
    }

    override fun setPickMode() {
        setPickMode(TimePickerPopup.Mode.YMD)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        TheRouter.inject(this)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        userUid?.let {
            uid = it
        } ?: kotlin.run {
            toastCenter("请登录")
            return null
        }
        accessToken?.let {
            token = it
        } ?: kotlin.run {
            toastCenter("请传入token")
            return null
        }

        Log.e(TAG, "userUid:$userUid,accessToken:$accessToken,top:$topPadding")
        return ComposeView(requireContext()).apply {
            setContent {
                CheckInMainView(
                    currentDate = currentDate,
                    topPadding = topPadding.pxToDp(),
                    showDatePicker = {
                        showDatePicker(currentDate.longValue.formatTimeYMD())
                    }
                )
            }
        }
    }
}